package com.shangpintechnology.sci.cam;

import java.io.*;

import java.awt.*;

import javax.swing.JFileChooser;
import javax.imageio.ImageIO;
import javax.media.*;

import javax.media.control.*;

import javax.media.protocol.*;
import javax.media.util.BufferToImage;

import javax.media.format.VideoFormat;

import javax.swing.JPanel;

import javax.swing.JToolBar;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.WindowConstants;

import java.awt.BorderLayout;

import java.awt.event.*;
import java.awt.image.BufferedImage;

public class MainFrame extends JFrame {
	 private static final long serialVersionUID = 7040032736336372576L;

	private JPanel centerPanel;

	public JPanel getCenterPanel() {
		return centerPanel;
	}

//	private JToolBar mainToolBar;

	private static JPanel northPanel;

	public void setNorthPanel(JPanel northPanel) {
		this.northPanel = northPanel;
	}

	public JPanel getNorthPanel() {
		return northPanel;
	}

//	private JButton recordButton;

	/** * 拍照按钮 */
//	private JButton capture = null;

//	private Buffer buf = null;

//	private BufferToImage btoi = null;

//	private Image img = null;
//	private ImagePanel imgpanel = null;
	private CamDataSource dataSource;

	private static DataSource camSource;

	public static DataSource getCamSource() {
		return camSource;
	}

	private static Processor processor;
	private static MainFrame mainFrame;
	public static MainFrame getMainFrame() {
		if(mainFrame==null){
			mainFrame = new MainFrame();
		}
		return mainFrame;
	}
	public static Processor getProcessor(DataSource camSource) {
//		Processor processor = null;
		if(processor==null){
			try {

				processor = Manager.createProcessor(camSource);

			} catch (IOException e) {

				JOptionPane.showMessageDialog(MainFrame.getMainFrame(),

				"Exception creating processor: " + e.getMessage(), "Error",
						JOptionPane.WARNING_MESSAGE);

				return null;

			} catch (NoProcessorException e) {

				JOptionPane.showMessageDialog(MainFrame.getMainFrame(),

				"Exception creating processor: " + e.getMessage(), "Error",
						JOptionPane.WARNING_MESSAGE);

				return null;

			}
			playhelper = new CamStateHelper(processor);

			if (!playhelper.configure(10000)) {

				JOptionPane.showMessageDialog(MainFrame.getMainFrame(),

				"cannot configure processor", "Error", JOptionPane.WARNING_MESSAGE);

				return null;

			}

			processor.setContentDescriptor(null);

			if (!playhelper.realize(10000)) {

				JOptionPane.showMessageDialog(MainFrame.getMainFrame(),

				"cannot realize processor", "Error", JOptionPane.WARNING_MESSAGE);

				return null;

			}

			processor.start();
			//northPanel.add(processor.getVisualComponent());// 视频面板
		}
		return processor;
	}
	//private static JPanel jPanel2;
	

	//public static JPanel getJPanel2() {
	//	return jPanel2;
	//}

	private static CamStateHelper playhelper;

	public MainFrame() {

	}

	private static DataSource camSource2 ;
	public MainFrame(CamDataSource dataSource) {
		northPanel = new JPanel();
		//centerPanel = new JPanel();
		this.dataSource = dataSource;

		this.dataSource.setParent(this);
		System.out.println("1111111111111112");
		camSource = dataSource.cloneCamSource();
		camSource2 = dataSource.cloneCamSource();
		initComponents();
		northPanel.add(getProcessor(MainFrame.getCamSource()).getVisualComponent());// 视频面板
		//centerPanel.add(getProcessor(MainFrame.getCamSource2()).getVisualComponent());
		System.out.println("1111111111111113");
	}
 public static DataSource getCamSource2(){
	return camSource2;
 }
	public void initCam() {

		CamDataSource dataSource = new CamDataSource(null);

		dataSource.setMainSource();

		dataSource.makeDataSourceCloneable();

		dataSource.startProcessing();

		MainFrame frame = new MainFrame(dataSource);
		System.out.println("111111111111111");

		frame.pack();
		frame.setSize(new Dimension(400, 400));

		//frame.setLocationRelativeTo(null);

		//frame.setVisible(true);

	}

	// 界面初始化

	private void initComponents() {

		northPanel = new JPanel();

		northPanel.setLayout(new BorderLayout());

		getContentPane().add(northPanel, BorderLayout.CENTER);
		pack();

	}
	// 显示所拍摄照片
	private BufferToImage btoi = null;
	Buffer buf;
	private Image img = null;
	private ImagePanel imgpanel = null;
	public ImagePanel recordButtonActionPerformed(java.awt.event.ActionEvent evt) {
			imgpanel = new ImagePanel();
			FrameGrabbingControl fgc = (FrameGrabbingControl) processor
					.getControl("javax.media.control.FrameGrabbingControl");
			buf = fgc.grabFrame();//Convert it to an image
			btoi = new BufferToImage((VideoFormat) buf.getFormat());
			img = btoi.createImage(buf);//show the image
			imgpanel.setImage(img); //save image
			return imgpanel;
	}
	/**
	 * 
	 * 拍照完成后显示照片的组件，可以拖动范围框，选择要截取的部分
	 */
	private int imgWidth = 500;

	private int imgHeight = 500;
	private int rectWidth = 200;

	private int rectHeight = 200;
	private int rectX;

	private int rectY;
	class ImagePanel extends Panel implements MouseMotionListener {
		private static final long serialVersionUID = -7758515478075258357L;
		private Image myimg = null;

		public ImagePanel() {
			setLayout(null);
			setSize(imgWidth, imgHeight);
		}

		public void setImage(Image img) {
			this.myimg = img;
			repaint();
		}

		public void update(Graphics g) {
			g.clearRect(0, 0, getWidth(), getHeight());
			if (myimg != null) {
				g.drawImage(myimg, 0, 0, this);
				g.setColor(Color.RED);
				g.drawRect(rectX, rectY, rectWidth, rectHeight);
			}
		}

		public void paint(Graphics g) {
			update(g);
		}

		public void mouseDragged(MouseEvent e) {
			rectX = e.getX() - 50;
			rectY = e.getY() - 50;
			repaint();
		}

		public void mouseMoved(MouseEvent e) {
		}
	}


}
